/*
 * Copyright 2008-2009 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public Licence Version 3
 * The licence can be read in its entirety in the LICENSE.txt file accompaning this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of WeightWatch.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package net.cachapa.weightwatch;

public class Trend {

	public static double getTrend(Value prevValue, Value value) {
		// Formula for calculating the trend: Tn=Tprev+Power(Wn−Tnprev)
		double trend, prevTrend, power = 0.1;
		if (prevValue == null) {
			return value.weight;
		}
		prevTrend = prevValue.trend;
		int days = prevValue.date.differenceInDays(value.date);
		power = Math.min(days * power, 1);
		trend = prevTrend + power * (value.weight - prevTrend);
		return trend;
	}
}
